package wifiunion.com.notice.widget;

import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


import java.util.HashMap;

import wifiunion.com.notice.BaseApplication;
import wifiunion.com.notice.BasePresenter;
import wifiunion.com.notice.CommonUtils;
import wifiunion.com.notice.Constants;
import wifiunion.com.notice.R;
import wifiunion.com.notice.activity.HomePageActivity;
import wifiunion.com.notice.utils.SharedPreferencesUtils;

/**
 * Created by Administrator on 2016/12/12.
 * 密码设置页面
 */

public class AboutDialog extends Dialog  {

    private Context context;
    private TextView titleName;
    private TextView cancelTxt;
    private TextView confirmTxt;
    private EditText mOlderEt, mNewPwdEt, mReNewPwdEt;

    private RelativeLayout mMainRl, mModifyRl, mAboutRl;

    private TextView mExitTv,mAboutTv, mCancelTv, mSupportTv;

    private String mOlderStr, mNewPwdStr, mReNewPwdStr;

    private TextView mVersionTv, mAboutCancelTv;


    private Handler mHandler = new Handler();

    private String mAuthtoken;

    HashMap<String, Object> mParams = new HashMap<String, Object>();


    public void setDialogTitle(String title) {
        titleName.setText(title);
    }

    public AboutDialog(final Context context) {
        super(context, R.style.alert_dialog);
        this.context = context;
        LayoutInflater li = LayoutInflater.from(context);
        ViewGroup contentView = (ViewGroup) li.inflate(
                R.layout.dialog_about, null);
        contentView.bringToFront();


        mAuthtoken = SharedPreferencesUtils.getSharedPreferences(Constants.CONFIG_AUTHTOKEN, getContext());
        titleName = (TextView) contentView.findViewById(R.id.tv_title_name);
        mMainRl = (RelativeLayout) contentView.findViewById(R.id.rl_selectcontent);
        mExitTv = (TextView) contentView.findViewById(R.id.tv_exitchoice);
        mAboutTv = (TextView) contentView.findViewById(R.id.tv_aboutchoice);
        mSupportTv = (TextView) contentView.findViewById(R.id.support_tv);

        String commpanyname = context.getResources().getString(R.string.commpanyname);
        if ("公司名称占位文案".equals(commpanyname) || TextUtils.isEmpty(commpanyname)) {
            mSupportTv.setText("");
        } else {
            mSupportTv.setText(commpanyname);
        }

        mCancelTv = (TextView) contentView.findViewById(R.id.tv_cancel);

        mAboutRl = (RelativeLayout) contentView.findViewById(R.id.rl_aboutcontent);
        mVersionTv = (TextView) contentView.findViewById(R.id.version_tv);
        mVersionTv.setText(CommonUtils.getVersionName(context));

        mAboutCancelTv = (TextView) contentView.findViewById(R.id.about_cancel_tv);
        mAboutCancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mAboutRl.setVisibility(View.GONE);
                mMainRl.setVisibility(View.VISIBLE);
                titleName.setText("设置");
            }
        });

        mAboutTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mMainRl.setVisibility(View.GONE);
                mAboutRl.setVisibility(View.VISIBLE);
                titleName.setText("关于我们");
            }
        });
        mExitTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                SharedPreferencesUtils.setUserSharedPreferences(null, getContext());
                SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_AUTHTOKEN, "", getContext());
                SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_PWD, "", getContext());
//                SharedPreferencesUtils.setSharedPreferences(Constants.CONFIG_USERNAME, "", getContext());
                dismiss();
                try {
                    BaseApplication.mApplication.clearAllList();
                } catch (Exception e) {
                    e.printStackTrace();
                }
                ((HomePageActivity) context).finish();

            }
        });


        mCancelTv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dismiss();
            }
        });


        mModifyRl = (RelativeLayout) contentView.findViewById(R.id.rl_setcontent);
        cancelTxt = (TextView) contentView.findViewById(R.id.cancel_txt);
        confirmTxt = (TextView) contentView.findViewById(R.id.confirm_txt);
        mOlderEt = (EditText) contentView.findViewById(R.id.et_oldpwd);
        mNewPwdEt = (EditText) contentView.findViewById(R.id.et_validatecode);
        mReNewPwdEt = (EditText) contentView.findViewById(R.id.et_resetnewpwd);

        cancelTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                dismiss();
            }
        });

        confirmTxt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
//                mOlderStr = mOlderEt.getEditableText().toString();
//                mNewPwdStr = mNewPwdEt.getEditableText().toString();
//                mReNewPwdStr = mReNewPwdEt.getEditableText().toString();
//                if (isStyleCorrect()) {
//
//                    mUpdatePwdPresenter = new ModifyPwdPresenter(getContext(), PwdSettingDialog.this);
//                    mParams.clear();
//                    mParams.put("Authorization", mAuthtoken);
//                    mParams.put("oldPassword", mOlderStr);
//                    mParams.put("password", mNewPwdStr);
//                    mUpdatePwdPresenter.start(mParams);
//                }

            }
        });


        setCanceledOnTouchOutside(false);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.gravity = Gravity.CENTER;
        onWindowAttributesChanged(lp);
        setContentView(contentView);


    }


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        WindowManager.LayoutParams lp = getWindow().getAttributes();
        lp.width = getContext().getResources().getDimensionPixelSize(
                R.dimen.dp_480);
        lp.height = (int) (CommonUtils.getScreenHeight(getContext()) * 0.8f);
        lp.gravity = Gravity.CENTER;
        getWindow().setAttributes(lp);
    }


    private boolean isStyleCorrect() {
        if (TextUtils.isEmpty(mOlderStr) || TextUtils.isEmpty(mNewPwdStr) || TextUtils.isEmpty(mReNewPwdStr)) {
            Toast.makeText(context, "原始密码，新密码，确认密码请输入完整", Toast.LENGTH_SHORT).show();
            return false;
        } else if (mNewPwdStr.length() < 6 || mReNewPwdStr.length() < 6) {
            Toast.makeText(context, "新密码长度必须大于等于6位", Toast.LENGTH_SHORT).show();
            return false;
        } else if (!mNewPwdStr.equals(mReNewPwdStr)) {
            Toast.makeText(context, "密码，确认密码输入不一致", Toast.LENGTH_SHORT).show();
            return false;
        } else {
            return true;
        }
    }




}